//
//  ZYCFliterMultipleChoiceView.m
//  ZDSC
//
//  Created by ZDTX on 2021/9/9.
//

#import "ZYCFliterMultipleChoiceView.h"
#import "ZYCFliterMultipleChoiceCell.h"
@interface ZYCFliterMultipleChoiceView ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *selectArr;
@end
@implementation ZYCFliterMultipleChoiceView

- (void)refresh{
    [self.tableView reloadData];
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.tableView];
        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];

    }
    return self;
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZYCRoleTreeModel *model = self.dataSource[indexPath.row];
    ZYCFliterMultipleChoiceCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ZYCFliterMultipleChoiceCell class]) forIndexPath:indexPath];
    cell.model = model;
    if ([self.selectArr containsObject:model]) {
        cell.select = YES;
    } else {
        cell.select = NO;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    ZYCFliterMultipleChoiceCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    ZYCRoleTreeModel *model = self.dataSource[indexPath.row];
    if (cell.select) {
        cell.select = NO;
        [self.selectArr removeObject:model];
    }else{
        cell.select = YES;
        [self.selectArr addObject:model];
    }
    
    NSLog(@"搜索选择的:%zd",self.selectArr.count);
    if ([self.delegate respondsToSelector:@selector(fliterMultipleChoiceView:selectArr:)]) {
        [self.delegate fliterMultipleChoiceView:self selectArr:self.selectArr];
    }
        
    [self.tableView reloadData];
}

#pragma mark - Table View Delegate
//-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
//    UILabel * tempLab = [[UILabel alloc] initWithFrame:CGRectMake(30, 0, kScreenWidth, 30)];
////    tempLab.textAlignment = NSTextAlignmentCenter;
//    tempLab.text = _searchActive ? _requltIndexData[section] : _tableIndexData[section];
//    tempLab.backgroundColor = [UIColor colorWithRed:243/255.0 green:243/255.0 blue:243/255.0 alpha:1.0];
//    return tempLab;
//}


-(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 48.0f;
}

-(CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 48.0f;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[ZYCFliterMultipleChoiceCell class] forCellReuseIdentifier:NSStringFromClass([ZYCFliterMultipleChoiceCell class])];
        if (@available(iOS 15.0, *)) {
            _tableView.sectionHeaderTopPadding = 0;
        }
    }
    return _tableView;
}

- (NSMutableArray *)selectArr {
    if (!_selectArr) {
        _selectArr = [NSMutableArray arrayWithCapacity:0];
    }
    return _selectArr;
}


@end
